package com.cdut.hwadeefsc.mapper;

import com.cdut.hwadeefsc.entity.homework;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface
homeworksMapper {
    @Select("SELECT * FROM homework WHERE class_id = #{classId} ORDER BY publish_date DESC")
    List<homework> findByClassId(Integer classId);

    @Select("SELECT * FROM homework WHERE id = #{id}")
    homework findById(Integer id);

    @Insert("INSERT INTO homework (class_id, teacher_id, course_name, homework_title, homework_content, " +
            "publish_date, deadline, attachment_url) " +
            "VALUES (#{classId}, #{teacherId}, #{courseName}, #{homeworkTitle}, #{homeworkContent}, " +
            "#{publishDate}, #{deadline}, #{attachmentUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(homework homework);

    @Update("UPDATE homework SET homework_title = #{homeworkTitle}, homework_content = #{homeworkContent}, " +
            "deadline = #{deadline}, attachment_url = #{attachmentUrl} WHERE id = #{id}")
    int update(homework homework);

    @Delete("DELETE FROM homework WHERE id = #{id}")
    int delete(Integer id);

    @Select("SELECT h.* FROM homework h " +
            "JOIN class_students cs ON h.class_id = cs.class_id " +
            "WHERE cs.student_id = #{studentId} ORDER BY h.publish_date DESC")
    List<homework> findByStudentId(Integer studentId);

    @Select("SELECT * FROM homework WHERE course_name = #{courseName} ORDER BY publish_date DESC")
    List<homework> findByCourseName(String courseName);

    @Select("SELECT * FROM homework WHERE deadline > NOW() ORDER BY deadline ASC")
    List<homework> findActiveHomework();
}




